﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
  
namespace Chuong02
{
    class MyModel
    {
        private Vector3 position;

        public Vector3 Position
        {
            get { return position; }
            set { position = value; }
        }
        private Vector3 rotation;

        public Vector3 Rotation
        {
            get { return rotation; }
            set { rotation = value; }
        }
        private Vector3 scale;

        public Vector3 Scale
        {
            get { return scale; }
            set { scale = value; }
        }
        private Model model;

        public Model Model
        {
            get { return model; }
            set { model = value; }
        }
       private Matrix[] modelTransforms;
       private GraphicsDevice graphicsDevice;
       private BoundingSphere boundingSphere;       //Khối cầu bao quanh model

        

        //Dat lại khối cầu  thông qua từng bước di chuyển của  Model
        //và củng được kéo dãn theo model

        //Dùng để xác định model có trên màn hình nữa hay không
        public BoundingSphere BoundingSphere
       {
           get 
           {
               //Kéo dãn-->vị trí mới
               Matrix worldTransform = Matrix.CreateScale(Scale) * Matrix.CreateTranslation(Position);

               BoundingSphere transformed = boundingSphere;
               transformed = transformed.Transform(worldTransform);

               return transformed;

           }
           set { boundingSphere = value; }
       }


       public MyModel(Model model, Vector3 position, Vector3 rotation , Vector3 scale,GraphicsDevice
            graphics)
       {
           this.Model = model;
           modelTransforms = new Matrix[Model.Bones.Count];
           Model.CopyAbsoluteBoneTransformsTo(modelTransforms);
           buildBoundingSphere();


           this.Position = position;
           this.Rotation = rotation;
           this.Scale = scale;
           graphicsDevice = graphics;

       }



        //Tính toán hình cầu bao quanh model
       private void buildBoundingSphere()
       {
           BoundingSphere sphere = new BoundingSphere(Vector3.Zero, 0);

           foreach (ModelMesh  mesh in Model.Meshes)
           {
                 BoundingSphere transformed = mesh.BoundingSphere.Transform( modelTransforms[mesh.ParentBone.Index]);
                 sphere = BoundingSphere.CreateMerged(sphere, transformed);
           }

           //Khối cầu  bao quanh model
           this.BoundingSphere = sphere;

       }



       public void Draw(Matrix view, Matrix Projection)
       {
           Matrix baseWord = Matrix.CreateScale(Scale) 
               * Matrix.CreateFromYawPitchRoll(Rotation.Y, Rotation.X, Rotation.Z) 
               * Matrix.CreateTranslation(Position);

           foreach (ModelMesh mesh in Model.Meshes)
           {

               Matrix localword = modelTransforms[mesh.ParentBone.Index] * baseWord;

               foreach (ModelMeshPart meshPart  in mesh.MeshParts)
               {
                   BasicEffect effect = (BasicEffect)meshPart.Effect;
                   effect.World = localword;
                   effect.View = view;
                   effect.Projection = Projection;
                   effect.EnableDefaultLighting();
                   
               }

               mesh.Draw();
           }

        
       }
    }                                 
}
